package cn.terminus.web.customer.customreceive.controller;

import cn.terminus.client.customer.customreceive.param.*;
import cn.terminus.client.customer.customreceive.result.CustomReceiveResult;
import cn.terminus.client.customer.customreceive.result.CustomReceiveZgResult;
import cn.terminus.client.customer.customreceive.service.CustomReceiveService;
import cn.terminus.result.common.exception.ServiceException;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.result.common.result.TerminusResult;
import cn.terminus.result.common.util.BeanCopyUtils;
import cn.terminus.web.customer.common.AbstractTerminusController;
import cn.terminus.web.customer.customreceive.form.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CustomReceive)控制层
 *
 * @author makejava
 * @version 1.0.0
 * @apiDefine CustomReceive $serviceComment
 * @since 2021-08-12 10:27:52
 */
@RestController
@RequestMapping("/customer/customReceive")
@Api(value = "客户接收相关接口", tags = "客户接受相关接口")
public class CustomReceiveController extends AbstractTerminusController {
    /**
     * 服务对象
     */
    @Resource
    private CustomReceiveService customReceiveService;


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "${serviceComment}新增", httpMethod = "POST")
    public TerminusResult<String> add(@RequestBody CustomReceiveNewForm form) {
        CustomReceiveNewParam param = BeanCopyUtils.copyProperties(form, CustomReceiveNewParam.class);
        try {
            return customReceiveService.add(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("新增${serviceComment}失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("新增${serviceComment}失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增客户接收", httpMethod = "POST")
    public TerminusResult<Boolean> insertBatch(@RequestBody List<CustomReceiveNewForm> form) {
        List<CustomReceiveNewParam> param = BeanCopyUtils.copy(form, CustomReceiveNewParam.class);
        try {
            return customReceiveService.insertBatch(param);
        } catch (ServiceException e) {
            logger.error("新增${serviceComment}失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("新增${serviceComment}失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }


    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "${serviceComment}编辑", httpMethod = "POST")
    public TerminusResult<Boolean> edit(@RequestBody CustomReceiveEditForm form) {
        CustomReceiveEditParam param = BeanCopyUtils.copyProperties(form, CustomReceiveEditParam.class);
        try {
            return customReceiveService.update(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("修改${serviceComment}失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("修改${serviceComment}失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }


    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ApiOperation(value = "批量删除", httpMethod = "POST")
    public TerminusResult<Boolean> delete(@RequestBody CustomReceiveDeleteForm form) {
        CustomReceiveDeleteParam param = BeanCopyUtils.copyProperties(form, CustomReceiveDeleteParam.class);
        try {
            return customReceiveService.delete(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("批量删除失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("批量删除失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }


    @RequestMapping(value = "/batchConfig", method = RequestMethod.POST)
    @ApiOperation(value = "批量确认", httpMethod = "POST")
    public TerminusResult<Boolean> batchConfig(@RequestBody BatchConfigForm form) {
        BatchConfigParam param = BeanCopyUtils.copyProperties(form, BatchConfigParam.class);
        try {
            return customReceiveService.batchConfig(param);
        } catch (ServiceException e) {
            logger.error("查询${serviceComment}失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("查询${serviceComment}失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }


    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiOperation(value = "${serviceComment}查询", httpMethod = "POST")
    public TerminusResult<List<CustomReceiveResult>> query(@RequestBody CustomReceiveQueryForm form) {
        CustomReceiveQueryParam param = BeanCopyUtils.copyProperties(form, CustomReceiveQueryParam.class);
        try {
            return customReceiveService.query(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("查询${serviceComment}失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("查询${serviceComment}失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }


    @PostMapping(value = "/pageWithZg")
    @ApiOperation(value = "分页查询客户接收", httpMethod = "POST")
    public TerminusResult<TerminusPageAble<CustomReceiveZgResult>> page(@RequestBody CustomReceiveQueryForm form) {
        CustomReceiveQueryParam param = BeanCopyUtils.copyProperties(form, CustomReceiveQueryParam.class);
        try {
            TerminusPageInfo page = new TerminusPageInfo(form.getCurrentPage(), form.getRowsOfPage());
            return customReceiveService.page(param, page, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("分页查询失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("分页查询失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

}
